///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_BBOX_PROTOCOL_CPACKET_H_
#define _BUOLA_BBOX_PROTOCOL_CPACKET_H_

#include <buola/io/cpacket.h>
#include <buola/bbox/protocol/enums.h>

namespace buola { namespace bbox { namespace protocol {

struct CHeader
{
    CHeader()=default;
    
    CHeader(EOpcode pMajor,uint16_t pMinor=0,uint32_t pSeqReply=0)
        :   mOpcode(static_cast<uint32_t>(pMajor)|((uint32_t)pMinor<<16))
        ,   mSeqReply(pSeqReply)
    {}
    
    EOpcode Major()     {   return static_cast<EOpcode>(mOpcode&0xffff);    }
    uint16_t Minor()    {   return mOpcode>>16;                             }
    bool IsError()      {   return Major()==EOpcode::ERROR;                 }
    void SetError(EError pError)    {   mOpcode=static_cast<uint32_t>(EOpcode::ERROR)|((uint32_t)pError<<16);  }
    
    uint32_t mSeqNumber;    //sequence number (starts at 0)
    uint32_t mOpcode;
    uint32_t mSeqReply;
    uint32_t mSize;         //total size of the packet, including header
};

typedef io::CPacket<CHeader> CPacket;
typedef io::CStackPacket<CHeader> CStackPacket;

class CReplyPacket : public CStackPacket
{
public:
    CReplyPacket(const CPacket &pReplyTo)
        :   CStackPacket(io::nHeader,EOpcode::REPLY,0,pReplyTo.Header().mSeqNumber)
    {}
};

class CErrorPacket : public CStackPacket
{
public:
    CErrorPacket(const CPacket &pReplyTo,EError pError)
        :   CStackPacket(io::nHeader,EOpcode::ERROR,(int)pError,pReplyTo.Header().mSeqNumber)
    {}
};

/*namespace protocol*/ } /*namespace bbox*/ } /*namespace buola*/ }

#endif
